

package com.eltsistemas.view.agendamento;

import com.eltsistemas.controller.ClienteController;
import com.eltsistemas.controller.ItemDoServicoController;
import com.eltsistemas.controller.ModeloController;
import com.eltsistemas.controller.PlacaController;
import com.eltsistemas.modelo.Cliente;
import com.eltsistemas.modelo.ItemDoServico;
import com.eltsistemas.modelo.Modelo;
import com.eltsistemas.modelo.Placa;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Frm_AgendamentoAdd extends javax.swing.JDialog {

    private static Frm_Agendamento frm_Agendamento;
    
    public Frm_AgendamentoAdd() {
        initComponents();
        setLocationRelativeTo(null);
        tamanhoTabela();
        carregaItem();
        carregaCliente();
    }
    
    private void carregaItem()
    {
        ItemDoServicoController itemController = new ItemDoServicoController();

        panel_Item.setLayout(new GridLayout(0, 3));
        panel_Item.setPreferredSize(new Dimension(12, 115));

        for(ItemDoServico i : itemController.lista())
        {
           panel_Item.add( new JCheckBox( i.getNome() ) );
        }
    }
    
    public void setObject(Frm_Agendamento frm_Agendamento)
    {
        this.frm_Agendamento = frm_Agendamento;
    }
    
    private void carregaCliente()
    {
        ClienteController clienteController = new ClienteController();
        List<Cliente> clienteList = clienteController.lista();
        
        cb_cliente.addItem("");
        
        for(Cliente c : clienteList)
        {
            cb_cliente.addItem(c.getRazao());
        }
    }

    private void tamanhoTabela()
    {
        DefaultTableCellRenderer centro = new DefaultTableCellRenderer();  
        centro.setHorizontalAlignment(SwingConstants.CENTER);  

        tabela.getColumnModel().getColumn( 0 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 1 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 2 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 3 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 4 ).setCellRenderer(centro);
        
        tabela.getColumnModel().getColumn( 0 ).setPreferredWidth( 20 );
        tabela.getColumnModel().getColumn( 1 ).setPreferredWidth( 20 );
        tabela.getColumnModel().getColumn( 2 ).setPreferredWidth( 50 ); 
        tabela.getColumnModel().getColumn( 3 ).setPreferredWidth( 60 ); 
        tabela.getColumnModel().getColumn( 4 ).setPreferredWidth( 40 ); 

    }
    
    private void buscaVeiculo()
    {
  
        String cliente = cb_cliente.getSelectedItem().toString();
        
        System.out.println("Valor: "+cliente);
        
        if(!cliente.isEmpty())
        {
            System.out.println("Teste: "+cliente);
            
            ModeloController modeloController = new ModeloController();
            PlacaController placaController = new PlacaController();

            List<Placa> placaList = placaController.buscaVeiculo(cliente);

            DefaultTableModel tableModel =(DefaultTableModel) tabela.getModel();

            int a = 0, b = 1;

            for(Placa p : placaList)
            {

                tableModel.setNumRows( b ) ;
                tabela.setValueAt(b, a, 0);
                tabela.setValueAt( p.getId(), a, 1 );
                tabela.setValueAt( p.getPlaca(), a, 2 );

                for(Modelo m : modeloController.lista())
                {
                    if(m.getId() == p.getModelo().getId())
                    {
                        tabela.setValueAt( m.getNome(), a, 3 );
                    }
                }

                tabela.setValueAt( p.getAnoModelo(), a, 4 );
                
                a++;
                b++;
            }

            tabela.getSelectionModel().clearSelection();
            tabela.repaint();
        }
        
        else
        {
            DefaultTableModel tableModel =(DefaultTableModel) tabela.getModel();
            
            while (tableModel.getRowCount() > 0)
            {   
                tableModel.removeRow( 0 );  
            }        
        }
    }  

     
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cb_cliente = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        tf_kmAtual = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_dataAtual = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tf_dataAtual1 = new javax.swing.JTextField();
        tf_kmAtual1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        panel_Item = new javax.swing.JPanel();
        btnOk = new javax.swing.JButton();
        btnCan = new javax.swing.JButton();

        jFormattedTextField1.setText("jFormattedTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agendamento | Novo");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Cliente");

        cb_cliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cb_clienteMouseClicked(evt);
            }
        });
        cb_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_clienteActionPerformed(evt);
            }
        });

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Cód.", "Placa", "Modelo", "Ano mod."
            }
        ));
        jScrollPane1.setViewportView(tabela);

        jLabel2.setText("Km atual");

        jLabel3.setText("Data");

        jLabel4.setText("Hora");

        jLabel5.setText("Km prog.");

        panel_Item.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Items do serviço"));

        javax.swing.GroupLayout panel_ItemLayout = new javax.swing.GroupLayout(panel_Item);
        panel_Item.setLayout(panel_ItemLayout);
        panel_ItemLayout.setHorizontalGroup(
            panel_ItemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 629, Short.MAX_VALUE)
        );
        panel_ItemLayout.setVerticalGroup(
            panel_ItemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(cb_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(tf_dataAtual, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel3))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel4)
                                            .addComponent(tf_dataAtual1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(tf_kmAtual, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(tf_kmAtual1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(0, 147, Short.MAX_VALUE))
                            .addComponent(jScrollPane1)
                            .addComponent(panel_Item, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_kmAtual1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cb_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tf_kmAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_dataAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_dataAtual1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_Item, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnOk.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnOk.setText("Ok");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnCan.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnCan.setText("Cancelar");
        btnCan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCan)
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btnCan))
                .addGap(0, 13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        //adiciona();
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnCanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCanActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCanActionPerformed

    private void cb_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_clienteActionPerformed
        buscaVeiculo();
    }//GEN-LAST:event_cb_clienteActionPerformed

    private void cb_clienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cb_clienteMouseClicked
       
    }//GEN-LAST:event_cb_clienteMouseClicked

   
    public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frm_AgendamentoAdd dialog = new Frm_AgendamentoAdd();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCan;
    private javax.swing.JButton btnOk;
    private javax.swing.JComboBox cb_cliente;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel panel_Item;
    private javax.swing.JTable tabela;
    private javax.swing.JTextField tf_dataAtual;
    private javax.swing.JTextField tf_dataAtual1;
    private javax.swing.JTextField tf_kmAtual;
    private javax.swing.JTextField tf_kmAtual1;
    // End of variables declaration//GEN-END:variables
}
